package tgc.edu.yzy.web.controller;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.yzy.custom.DatagridUtils;
import tgc.edu.yzy.entity.Test;
import tgc.edu.yzy.entity.Test01;
import tgc.edu.yzy.service.Test01Service;
import tgc.edu.yzy.service.TestService;

@Controller
public class TestController {
	@Autowired
	private TestService testservice;
	@Autowired
	private Test01Service test01;
	@RequestMapping(value="/",method=RequestMethod.GET)
	private String index() {
		return "login";
	}
	@RequestMapping(value="form")
	private String form() {
		return "form";
	}
	@RequestMapping(value="listfind")
	@ResponseBody
	private Object listfind() {
		List<Test01> test01s=test01.findAll();
		return test01s; 
	}
	@RequestMapping(value="add",method=RequestMethod.POST)
	@ResponseBody
	private Object add(Test test) {
		testservice.save(test);
		return "ok";
	}
	
	/***
	 * 进入form2表单
	 */
	@RequestMapping(value="form2")
	private String form2() {
		return "form2";
	}
	/***
	 * 进入form3表单
	 */
	@RequestMapping(value="form3")
	private String form3() {
		return "form3";
	}
	@RequestMapping(value="one7")
	private String wa() {
		return "07页面布局";
	}
	@RequestMapping(value="08datagrid")
	private String datagrid() {
		return "08datagrid";
	}
	@RequestMapping(value="09datagridPage")
	private String datagridPage() {
		return "09datagridPage";
	}
	@RequestMapping(value="10combobox")
	private String combobox() {
		return "10combobox";
	}
	@RequestMapping(value="11combogrid")
	private String combogrid() {
		return "11combogrid";
	}
	@RequestMapping(value="12datagridPage")
	private String datagridPage1() {
		return "12datagridPage";
	}
	@RequestMapping(value="tree13")
	private String tree13() {
		return "13tree";
	}
	/***
	 * Ajax获取所有数据 不分页
	 * @throws InterruptedException 
	 */
	@RequestMapping(value="testlist")
	@ResponseBody
	private Object testlist(Integer page,Integer rows) throws InterruptedException {
		/*Thread.sleep(2000);*/
		/*Pageable pageable=PageRequest.of(page, rows);
		Page<Test> test=testservice.findAll(pageable);*/
		List<Test> test=testservice.findAll();
		return test; 
	}
	/***
	 * 分页显示
	 * @param form
	 * @return
	 * @throws InterruptedException
	 */
	@RequestMapping(value="testpage")
	@ResponseBody
	private Object testpage(DatagridUtils form,Test test1) throws InterruptedException {
		//判断是否需要进行排序
		Pageable pageable=form.buildPageable();
		//进行分页 + 查询
		Page<Test> test=null;
		Specification<Test> spec = new Specification<Test>() {

			@Override
			public Predicate toPredicate(Root<Test> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				HashSet<Predicate> rules = new HashSet<>();
				if(StringUtils.hasText(test1.getName())) {
					rules.add(criteriaBuilder.like(root.get("name"), "%"+test1.getName()+"%"));
				}else if(StringUtils.hasText(test1.getEndtime())) {
					rules.add(criteriaBuilder.like(root.get("lesseeAdmin").get("unit"), "%"+test1.getEndtime()+"%"));
				}
				return criteriaBuilder.and(rules.toArray(new Predicate[rules.size()]));
			}			
		};
		test= testservice.findAll(spec,pageable);
		HashMap<String , Object> result = form.getPageResult(test);
		return result; 
	}
	/***
	 * 删除 批量删除
	 * @param ids
	 */
	@RequestMapping(value="delete")
	@ResponseBody
	private Object delete(String ids) {
		String[] id=ids.split(",");
		for (int i = 0; i < id.length; i++) {
			testservice.deleteByID(Integer.parseInt(id[i]));
		}
		return "";
	}
}
